//
//  IMImageReceiveCell.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/7/6.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "IMImageReceiveCell.h"
#import <YBImageBrowser/YBImageBrowser.h>

#define kImageMaxHeight kWidth(120)

@interface IMImageReceiveCell ()<YBImageBrowserDataSource>

@property (nonatomic, strong) UIImageView *icon;
//@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UIView *messageContentView;
@property (nonatomic, strong) UIImageView *messageImgView;
@property (nonatomic, strong) UILabel *dtLabel;

@end

@implementation IMImageReceiveCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubViews];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)setMessage:(IMMessageEntity *)message
{
    _message = message;
    [self.messageImgView sd_setImageWithURL:[NSURL URLWithString:message.filePath] placeholderImage:PlaceHolderImage];
    self.dtLabel.text = message.dt;
//    [self.messageImgView sd_setImageWithURL:[NSURL URLWithString:message.filePath] placeholderImage:PlaceHolderImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//
//    }];
}

- (void)setupSubViews
{
    [self.contentView addSubview:self.icon];
    //    [self addSubview:self.nameLab];
    [self.contentView addSubview:self.messageContentView];
    [self.messageContentView addSubview:self.messageImgView];
    [self.contentView addSubview:self.dtLabel];
    
    self.messageContentView.backgroundColor = [UIColor whiteColor];
    
    [self.dtLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.left.right.equalTo(self.contentView);
    }];
    
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(kWidth(15));
        make.top.equalTo(self.dtLabel.mas_bottom).offset(kWidth(10));
        make.width.height.equalTo(@(kWidth(40)));
    }];
    
    //    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.equalTo(self.icon);
    //        make.left.equalTo(self.icon.mas_right).offset(kWidth(6));
    //    }];
    
    [self.messageContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.left.equalTo(self.nameLab);
        make.left.equalTo(self.icon.mas_right).offset(kWidth(6));
        //        make.top.equalTo(self.nameLab.mas_bottom).offset(kWidth(6));
        make.top.equalTo(self.icon);
        make.right.lessThanOrEqualTo(self.contentView).offset(-kWidth(kScreenW/4.0));
        make.height.lessThanOrEqualTo(@(kImageMaxHeight));
        make.bottom.equalTo(self.contentView);
    }];
    
    [self.messageImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.messageContentView).offset(kWidth(0));
        make.bottom.right.equalTo(self.messageContentView).offset(-kWidth(0));
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(browserImage:)];
    [self.messageImgView addGestureRecognizer:tap];
}

- (void)browserImage:(UITapGestureRecognizer *)tap
{
    [self browserImage];
}

// 图片预览
- (void)browserImage
{
    YBImageBrowser *browser = [YBImageBrowser new];
    browser.dataSource = self;
    browser.currentIndex = 0;
    [browser show];
}

#pragma mark - YBImageBrowserDataSource
- (NSUInteger)yb_numberOfCellForImageBrowserView:(YBImageBrowserView *)imageBrowserView {
    return 1;
}
- (id<YBImageBrowserCellDataProtocol>)yb_imageBrowserView:(YBImageBrowserView *)imageBrowserView dataForCellAtIndex:(NSUInteger)index {
    YBImageBrowseCellData *data = [YBImageBrowseCellData new];
    data.imageBlock = ^__kindof UIImage * _Nullable{
        return self.messageImgView.image;
    };
    data.sourceObject = self.messageImgView;
    return data;
}


- (UIImageView *)icon
{
    if (!_icon) {
        _icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"person_headpic"]];
        _icon.layer.cornerRadius = kWidth(20);
        _icon.layer.masksToBounds = YES;
        _icon.backgroundColor = [UIColor whiteColor];
    }
    return _icon;
}

//- (UILabel *)nameLab
//{
//    if (!_nameLab) {
//        _nameLab = [[UILabel alloc] init];
//        _nameLab.textColor = kColor666666;
//        _nameLab.font = [UIFont systemFontOfSize:12];
//    }
//    return _nameLab;
//}

- (UIView *)messageContentView
{
    if (!_messageContentView) {
        _messageContentView = [[UIView alloc] init];
    }
    return _messageContentView;
}

- (UIImageView *)messageImgView
{
    if (!_messageImgView) {
        _messageImgView = [[UIImageView alloc] init];
        _messageImgView.userInteractionEnabled = YES;
        _messageImgView.contentMode = UIViewContentModeScaleAspectFill;
        _messageImgView.layer.masksToBounds = YES;
    }
    return _messageImgView;
}

- (UILabel *)dtLabel
{
    if (!_dtLabel) {
        _dtLabel = [[UILabel alloc] init];
        _dtLabel.textColor = kColorAAAAAA;
        _dtLabel.font = [UIFont systemFontOfSize:12];
        _dtLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _dtLabel;
}

@end
